#!/usr/bin/env python
# -*- coding:utf-8 -*- #
from popcan.helper.requesthelper import getParam as _p,respError,JsonResult
from popcan.helper.validhelper import validEmail,validNick,validPasswd
from popcan.db.daos import CustomerDao
from popcan.biz.accessors import CustomerAccessor,SessionAccessor
from popcan.helper.viewhelper import require_login
import hashlib
import threading
from popcan.globals.settings import ServerSettings

def register(request):
    email = _p(request,'email','').strip()
    nick = _p(request,'nick','').strip()
    passwd = _p(request,'passwd','').strip()
    if not email:
        return respError('email_empty','email must not be empty')
    if not nick:
        return respError('nick_empty','nick must not be empty')
    if not passwd:
        return respError('passwd_empty','passwd must not be empty')
    if not validEmail(email):
        return respError('email_illegal','email format illegal')
    if not validNick(nick):
        return respError('nick_illegal','nick format illegal')
    if not validPasswd(passwd):
        return respError('passwd_illegal','passwd format illegal')
    if CustomerDao().existsEmail(email):
        return respError('email_exists','email already exists')
    if CustomerDao().existsNick(nick):
        return respError('nick_exists','nick already exists')
    passwd = hashlib.md5(passwd).hexdigest()
    customer = CustomerAccessor().createCustomer(email, nick, passwd)
    session = SessionAccessor().createSession(customer.custId)
    result = JsonResult()
    result.retdata['customerId'] = customer.custId
    response = result.response()
    response.set_cookie('session_id',session.sessionId)
    return response

def login(request):
    print 'server',ServerSettings.serverId,':thread',threading.currentThread().ident
    email = _p(request,'email','').strip()
    passwd = _p(request,'passwd','').strip()
    remoteip = request.META['REMOTE_ADDR'].strip()
    if not email:
        return respError('email_empty','email must not be empty')
    if not passwd:
        return respError('passwd_empty','passwd must not be empty')
    passwd = hashlib.md5(passwd).hexdigest()
    customer = CustomerAccessor().getCustomerByEmail(email)
    if not customer:
        return respError('email_wrong','email not exists')
    if customer.passwd <> passwd:
        return respError('passwd_wrong','email and passwd not match')
    cachedCustomer = CustomerAccessor().getCachedCustomer(customer.custId)
    if cachedCustomer:
        customer = cachedCustomer
    customer.ips.add(remoteip)
    customer = CustomerAccessor().updateLoginDate(customer)
    session = SessionAccessor().createSession(customer.custId,remoteip)
    result = JsonResult()
    result.retdata['cust_id'] = customer.custId
    response = result.response()
    response.set_cookie('session_id',session.sessionId)
    return response

@require_login
def modify(request):
    passwd = _p(request,'passwd','').strip()
    newpasswd = _p(request,'newpasswd','').strip()
    if not passwd:
        return respError('oldpasswd_empty','oldpasswd must not be empty')
    if not newpasswd:
        return respError('newpasswd_empty','newpasswd must not be empty')
    session = request.affix['session']
    custId = session.custId
    customer = CustomerAccessor().getCustomer(custId)
    if not customer:
        return respError('customer_notexists','customer not exists')
    if customer.passwd <> passwd:
        return respError('passwd_wrong','old passwd is not right')
    if not validPasswd(newpasswd):
        return respError('passwd_illegal','passwd format illegal')
    CustomerAccessor().updatePasswd(customer, passwd)
    result = JsonResult()
    return result.response()

def logout(request):
    sessionId = request.COOKIES.get('session_id',None)
    if sessionId:
        SessionAccessor().removeSession(sessionId)
    result = JsonResult()
    return result.response()
